package com.bckj.fastboot.iot.data.mysql.repository;

import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.core.page.PageSupport;
import com.bckj.fastboot.iot.common.dto.query.IotBasicDataQuery;
import com.bckj.fastboot.iot.common.dto.request.IotBasicDataRequest;
import com.bckj.fastboot.iot.common.dto.response.IotBasicDataResponse;
import com.bckj.fastboot.iot.common.repository.IotBasicDataRepository;
import com.bckj.fastboot.iot.data.mysql.convert.IotBasicDataConvert;
import com.bckj.fastboot.iot.data.mysql.entity.IotBasicData;
import com.bckj.fastboot.iot.data.mysql.mapper.IotBasicDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * IotBasicDataRepositoryImpl
 *
 * @author wfc
 **/
@Repository
@RequiredArgsConstructor
public class IotBasicDataRepositoryImpl
        extends CrudRepository<IotBasicDataMapper, IotBasicData>
        implements IotBasicDataRepository {

    private final IotBasicDataConvert iotBasicDataConvert;

    @Override
    public boolean save(IotBasicDataRequest request) {
        IotBasicData entity = iotBasicDataConvert.toEntity(request);
        entity.setDataValue(JsonUtils.toJsonStr(request.getDataMap()));
        return save(entity);
    }

    @Override
    public boolean updateById(IotBasicDataRequest request) {
        IotBasicData entity = iotBasicDataConvert.toEntity(request);
        return updateById(entity);
    }

    @Override
    public boolean removeById(Long id) {
        return super.removeById(id);
    }

    @Override
    public IotBasicDataResponse details(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<IotBasicDataResponse> findList(IotBasicDataQuery query) {
        return baseMapper.findList(query);
    }

    @Override
    public PageResult<IotBasicDataResponse> page(Pageable page, IotBasicDataQuery query) {
        var result = PageSupport.page(page, query, this::findList);
        return PageResult.of(result);
    }
}
